package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dto.pagedto.ReportPageDto;
import com.heima.mapper.ReportMapper;
import com.heima.service.ReportService;
import com.heima.vo.PageResult;
import com.heima.vo.reportvo.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final ReportMapper reportMapper;

    /**
     * 客户统计
     * @param dto
     * @return
     */
    @Override
    public PageResult contractStatisticsList(ReportPageDto dto) {

        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        //查询合同基础信息
        Page<ContractReportVo> list = reportMapper.queryContractList(dto);

        PageResult pageResult = new PageResult();
        pageResult.setRows(list.getResult());
        pageResult.setTotal(list.getTotal());
        return pageResult;
    }

    /**
     * 线索统计
     * @param dto
     * @return
     */
    @Override
    public PageResult cluesStatisticsLisList(ReportPageDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        //查询线索基础信息
        Page<ClueVo> list = reportMapper.queryClueList(dto);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list.getResult());
        pageResult.setTotal(list.getTotal());
        return pageResult;
    }

    /**
     * 销售统计报表
     * @param pageNum
     * @param pageSize
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageResult getSalesReport(Integer pageNum, Integer pageSize, String begin, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate beginDate = LocalDate.parse(begin, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);
        log.info("销售统计报表，查询时间：{} - {}",beginDate,endDate);
        PageHelper.startPage(pageNum,pageSize);
        Page<SalesReportVo> page =reportMapper.getSalesReport(beginDate,endDate);
        log.info("查询结果：{}",page.getResult());
        long total = page.getTotal();
        List<SalesReportVo> result = page.getResult();
        return new PageResult(total,result, HttpServletResponse.SC_OK,"查询成功",null);
    }

    /**
     * 客户数据可视化
     * @param begin
     * @param end
     * @return
     */
    @Override
    public REportVo contractStatisticsListByTime(LocalDate begin, LocalDate end) {
        REportVo rEportVo = new REportVo();
        List<LocalDate> allDay = getAllDays(begin,end);
        rEportVo.setXAxis(allDay);
        List<CountVo> countList = reportMapper.getCountList(begin,end.plusDays(1));
        List<Integer> newList = countList.stream().map(CountVo::getNewCount).collect(Collectors.toList());
        List<Integer> totalList = countList.stream().map(CountVo::getTotalCount).collect(Collectors.toList());
        XYVo xy1 = new XYVo();
        xy1.setName("新增客户数");
        xy1.setData(newList);
        XYVo xy2 = new XYVo();
        xy2.setName("总客户数");
        xy2.setData(totalList);
        List<XYVo> list = new ArrayList<>();
        list.add(xy1);
        list.add(xy2);
        rEportVo.setSeries(list);
        return rEportVo;
    }

    /**
     * 客户数据可视化
     * @param begin
     * @param end
     * @return
     */
    @Override
    public REportVo cluesStatisticsListListByTime(LocalDate begin, LocalDate end) {
        REportVo rEportVo = new REportVo();
        List<LocalDate> allDay = getAllDays(begin,end);
        rEportVo.setXAxis(allDay);
        List<CountVo> countList = reportMapper.getClueCountList(begin,end.plusDays(1));
        List<Integer> newList = countList.stream().map(CountVo::getNewCount).collect(Collectors.toList());
        List<Integer> totalList = countList.stream().map(CountVo::getTotalCount).collect(Collectors.toList());
        XYVo xy1 = new XYVo();
        xy1.setName("新增线索数量");
        xy1.setData(newList);
        XYVo xy2 = new XYVo();
        xy2.setName("线索总数量");
        xy2.setData(totalList);
        List<XYVo> list = new ArrayList<>();
        list.add(xy1);
        list.add(xy2);
        rEportVo.setSeries(list);
        return rEportVo;
    }

    /**
     * 活动饼图
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ActivityAndChannelNumNumVo activityNum(LocalDate begin, LocalDate end) {
        ActivityAndChannelNumNumVo voList = new ActivityAndChannelNumNumVo();
        List<ActivityNum> activityNumList = reportMapper.findActivityNumList(begin,end.plusDays(1));
        voList.setData(activityNumList);
        return voList;
    }

    /**
     * 渠道饼图
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ActivityAndChannelNumNumVo ChannelNum(LocalDate begin, LocalDate end) {
        ActivityAndChannelNumNumVo voList = new ActivityAndChannelNumNumVo();
        List<ActivityNum> activityNumList = reportMapper.findActivityNumList(begin,end.plusDays(1));
        voList.setData(activityNumList);
        return voList;
    }

    /**
     * 销售统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public REportVo salesStatisticsByTime(LocalDate begin, LocalDate end) {
        REportVo rEportVo = new REportVo();
        List<LocalDate> allDay = getAllDays(begin,end);
        List<XYVo> xyVoList = new ArrayList<>();
        List<Double> list = reportMapper.findSaleVo(begin,end.plusDays(1));
        System.out.println(list);
        rEportVo.setXAxis(allDay);
        XYVo vo = new XYVo();
        vo.setName("销售统计");
        vo.setData(list);
        xyVoList.add(vo);
        rEportVo.setSeries(xyVoList);
        return rEportVo;
    }


    /**
     * 计算开始到结束的所有日期
     */
    public List<LocalDate> getAllDays(LocalDate start,LocalDate end){
        List<LocalDate> list = new ArrayList<>();
        list.add(start);
        while (!start.equals(end)){
            start = start.plusDays(1);
            list.add(start);
        }
        return list;
    }

}
